'use strict';
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const P = require('../nodes/Pair.cjs'),
  A = require('../nodes/YAMLMap.cjs'),
  _ = require('./resolve-props.cjs'),
  S = require('./util-contains-newline.cjs'),
  N = require('./util-flow-indent-check.cjs'),
  L = require('./util-map-includes.cjs'),
  T = 'All mapping items must start at the same column';
function q({ composeNode: y, composeEmptyNode: M }, n, c, e, k) {
  var r;
  const g = (k == null ? void 0 : k.nodeClass) ?? A.YAMLMap,
    l = new g(n.schema);
  n.atRoot && (n.atRoot = !1);
  let s = c.offset,
    p = null;
  for (const I of c.items) {
    const { start: d, key: i, sep: f, value: m } = I,
      t = _.resolveProps(d, {
        indicator: 'explicit-key-ind',
        next: i ?? (f == null ? void 0 : f[0]),
        offset: s,
        onError: e,
        startOnNewline: !0,
      }),
      h = !t.found;
    if (h) {
      if (
        (i &&
          (i.type === 'block-seq'
            ? e(
                s,
                'BLOCK_AS_IMPLICIT_KEY',
                'A block sequence may not be used as an implicit map key',
              )
            : 'indent' in i && i.indent !== c.indent && e(s, 'BAD_INDENT', T)),
        !t.anchor && !t.tag && !f)
      ) {
        (p = t.end),
          t.comment &&
            (l.comment
              ? (l.comment +=
                  `
` + t.comment)
              : (l.comment = t.comment));
        continue;
      }
      (t.hasNewlineAfterProp || S.containsNewline(i)) &&
        e(
          i ?? d[d.length - 1],
          'MULTILINE_IMPLICIT_KEY',
          'Implicit keys need to be on a single line',
        );
    } else
      ((r = t.found) == null ? void 0 : r.indent) !== c.indent &&
        e(s, 'BAD_INDENT', T);
    const w = t.end,
      o = i ? y(n, i, t, e) : M(n, w, d, null, t, e);
    n.schema.compat && N.flowIndentCheck(c.indent, i, e),
      L.mapIncludes(n, l.items, o) &&
        e(w, 'DUPLICATE_KEY', 'Map keys must be unique');
    const a = _.resolveProps(f ?? [], {
      indicator: 'map-value-ind',
      next: m,
      offset: o.range[2],
      onError: e,
      startOnNewline: !i || i.type === 'block-scalar',
    });
    if (((s = a.end), a.found)) {
      h &&
        ((m == null ? void 0 : m.type) === 'block-map' &&
          !a.hasNewline &&
          e(
            s,
            'BLOCK_AS_IMPLICIT_KEY',
            'Nested mappings are not allowed in compact mappings',
          ),
        n.options.strict &&
          t.start < a.found.offset - 1024 &&
          e(
            o.range,
            'KEY_OVER_1024_CHARS',
            'The : indicator must be at most 1024 chars after the start of an implicit block mapping key',
          ));
      const u = m ? y(n, m, a, e) : M(n, s, f, null, a, e);
      n.schema.compat && N.flowIndentCheck(c.indent, m, e), (s = u.range[2]);
      const C = new P.Pair(o, u);
      n.options.keepSourceTokens && (C.srcToken = I), l.items.push(C);
    } else {
      h &&
        e(
          o.range,
          'MISSING_CHAR',
          'Implicit map keys need to be followed by map values',
        ),
        a.comment &&
          (o.comment
            ? (o.comment +=
                `
` + a.comment)
            : (o.comment = a.comment));
      const u = new P.Pair(o);
      n.options.keepSourceTokens && (u.srcToken = I), l.items.push(u);
    }
  }
  return (
    p && p < s && e(p, 'IMPOSSIBLE', 'Map comment with trailing content'),
    (l.range = [c.offset, s, p ?? s]),
    l
  );
}
exports.resolveBlockMap = q;
